﻿
//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

#include "GRepHolder.h"
#include "GbmpGuid.h"
#include "OwnerPtr.h"
#include "UniIdentity.h"
#include "DbObjectUtils.h"
#include "IGenericElement.h"
#include "IElementModelShape.h"
#include "IGraphicsElementShape.h"
#include "IElementParentReporter.h"
#include "IElementBasicInformation.h"
#include "ImplementationUidTreeRegister.h"
#include "GcmpBuiltInCategoryUniIdentities.h"

#include "GRepHolderUserTransformationComponent.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

using namespace gcmp;
using namespace Sample;

DBOBJECT_DATA_DEFINE(GRepHolder)
{
    m_pOwnerElement = nullptr;
}

GRepHolder::~GRepHolder()
{
}

GRepHolder * Sample::GRepHolder::Create(gcmp::IDocument * pDocument, OwnerPtr<gcmp::IGraphicsElementShape> opGRep)
{
    OwnerPtr<GRepHolder> opHolder = NEW_AS_OWNER_PTR(GRepHolder);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opHolder, L"创建opHolder失败",L"GDMP",L"2024-03-30");

    GRepHolder* pHolder = opHolder.get();

    // 类别为BICU_ELEMENT_GREP_HOLDER，用于做几何和显示共享
    IGenericElement* pGenericElement = IGenericElement::Create(
        pDocument, TransferOwnership(opHolder),
        GRepHolder::GetClassId().GetGuid(),
        BuiltInCategoryUniIdentities::BICU_ELEMENT_GREP_HOLDER,
        ElementCreationOptions::Normal,
        gcmp::GuidUtils::GetInvalid(),
        false);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pGenericElement, L"创建IGenericElement失败",L"GDMP",L"2024-03-30");

    IElementBasicInformation* pBasicInformation = pGenericElement->GetBasicInformation();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pBasicInformation, L"pBasicInformation为空",L"GDMP",L"2024-03-30");
    pBasicInformation->SetImplementationUid(GRepHolder::GetImplementationUid());

    IElementModelShape* pModelShape = pGenericElement->GetElementModelShape();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pModelShape, L"pModelShape为空",L"GDMP",L"2024-03-30");
    pModelShape->SetGraphicsElementShape(TransferOwnership(opGRep));

    return pHolder;
}

gcmp::UniIdentity Sample::GRepHolder::GetImplementationUid()
{
    static UniIdentity s_implementationUid = UniIdentity::Create(
        GuidUtils::FromString(L"{FBBFAF03-AEB0-4969-8010-819CECA1C4E6}"), L"GRepHolder");
    return s_implementationUid;
}

gcmp::NdbObject* GRepHolder::GetTopOwnerObject() const
{
    return quick_cast<gcmp::NdbObject>(m_pOwnerElement);
}

bool Sample::GRepHolder::FinishEditing()
{
    return true;
}

gcmp::IElement* Sample::GRepHolder::GetOwnerElement()
{
    return m_pOwnerElement;
}

void Sample::GRepHolder::ReportParents(IElementParentReporter& elementParentReporter) const
{
    const IElement* pElement = GetOwnerElement();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pElement, L"IElement为空", L"GDMP", L"2023-09-30");
    elementParentReporter.ReportStrong(pElement->GetElementId());
}

bool Sample::GRepHolder::SetOwnerElement(IElement* pOwnerElement)
{
    m_pOwnerElement = pOwnerElement;
    return true;
}

void Sample::GRepHolder::UpdateForWeakParentDeletion(const std::set<ElementId>& deletedElementIds)
{
}

const gcmp::IElement* Sample::GRepHolder::GetOwnerElement() const
{
    return m_pOwnerElement;
}

REGISTER_IMPLEMENTATION_UID_TREE_RELATION(IGenericElement, GRepHolder)

